package cn.hx.admin.controller;

import cn.hx.admin.entity.City;
import cn.hx.admin.service.CityService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (City)表控制层
 *
 * @author makejava
 * @since 2023-12-12 18:22:18
 */
@RestController
@RequestMapping("city")
public class CityController {
    /**
     * 服务对象
     */
    @Resource
    private CityService cityService;

    /**
     * 分页查询
     *
     * @param city 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<List<City>> queryByPage(City city) {
        return ResponseEntity.ok(this.cityService.queryByPage(city));
    }

    @GetMapping("withCount")
    public ResponseEntity<List<City>> queryByPage() {
        return ResponseEntity.ok(this.cityService.queryAllWithCount());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<City> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.cityService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param city 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<City> add(@RequestBody City city) {
        return ResponseEntity.ok(this.cityService.insert(city));
    }

    /**
     * 编辑数据
     *
     * @param city 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<City> edit(@RequestBody City city) {
        return ResponseEntity.ok(this.cityService.update(city));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.cityService.deleteById(id));
    }

}

